﻿var pageNo=1;
var searchPriceAjax;
var searchPricetype=true;//是否查询价格
var loadresultAjaxRun=false;
var defaultkw="输入产品描述或元器件型号";
$(document).ready(function () {
    setTimeout(function(){getPrice(1,0);},20);
    $(window).scroll(function(){
        var marginBot=0;
        var show="";
        if(document.documentElement.scrollTop){
            marginBot=document.documentElement.scrollHeight-(document.documentElement.scrollTop+document.body.scrollTop)-document.documentElement.clientHeight;
            //show=document.documentElement.scrollHeight+"-("+document.documentElement.scrollTop+"+"+document.body.scrollTop+")-"+document.documentElement.clientHeight+"="+marginBot;
        }else{
            marginBot=document.body.scrollHeight-document.body.scrollTop-document.documentElement.clientHeight;
            //marginBot=document.body.scrollHeight-((document.body.scrollHeight-document.body.clientHeight)+document.body.scrollTop);
            //show=document.documentElement.scrollHeight+":"+document.body.clientHeight+"-"+document.body.scrollTop+"-"+document.body.scrollHeight+":"+document.documentElement.clientHeight+"="+marginBot;
        }
        //$("#searchBox").val(show);
        if(marginBot<=0){
            if(loadresultAjaxRun==false && pageNo<maxpage){
                loadresultAjaxRun=true;
                loadresultAjax();
            }
        }
//        if(($("body").height()-$("body,html").scrollTop())<=document.documentElement.clientHeight){
//            if(loadresultAjaxRun==false && pageNo<maxpage){
//                loadresultAjaxRun=true;
//                loadresultAjax();
//
//            }
//        }
        //var _v="可视:"+$(window).height()+"文档:"+$(document).height()+"body:"+$(document.body).height()+"卷起:"+marginBot;
        //$("#searchBox").val(_v);
        //$(".searchpage").each(function(index){
            //if($(this).find("span:eq(0)").text().indexOf("=")==-1){
                //$(this).find("span:eq(0)").text($(this)[0].offsetTop+"="+$(this).find("span:eq(0)").text());
            //}
            //$(this).find("span:eq(0)").text($(this)[0].getBoundingClientRect().top+"/");
        //});

        showPage();
    });
     
    $("body").bind("keyup",function(event) {
        var _v=arguments.callee.caller.arguments[0] || window.event;
        if(_v.keyCode==13){
            searchbtnsearch();
        }
    });
    if(maxpage<=1){
         $(".jiazai").css("display","none");
    }


    imgSize();
});
function showPage(){
    var _show="";
    $(".searchpage").each(function(index){
        var _w=$(window).height()/3*2;
        var _t=$(this)[0].getBoundingClientRect().top;
        if( _t>_w && _t<$(window).height()){
            _show=$(this).html();
        }
    });
    $(".searchpage2").html(_show);
}
//function test(){
//    var _str="";
//    $(".searchpage").each(function(index){
//        $(this).find("span:eq(0)").text($(this)[0].getBoundingClientRect().top+"/");
//        _str+= $(this).find("span:eq(0)").text()+"</br>";
//    });
//    alert(_str);
//}

function searchbtnsearch(){
//  var _kw=ToCDB($.trim($("#searchBox").val()));
//  if(_kw!=ToCDB(defaultkw)){
//		if(_kw.length>0){
//			//kwplus(_kw);
//			//kwhistoryadd(_kw);
//			if(_kw.indexOf('/')==-1){
//				window.location.href="/m/search/"+_kw;
//			}else{
//				window.location.href="/m/search?s="+_kw;
//			}
//		}
//	}
    var dayouzi = $("#searchBox").val();
    window.location.href = "/m?s=" + dayouzi;
}
//requestCount执行的总循环
function getPrice(requestCount,trid){
    //获取总行数
    var total=$("#main li").length;
    if(trid>=total){
        trid=1;
        requestCount++;
    }
    if(requestCount>3){
        return;
    }
    if(searchPricetype==false){
        return;
    }

    $("#main li:eq("+trid+")").each(function(index){

        var no=$.trim($(this).find(".right .right-top a:eq(0)").text());
        var manu_r= $.trim($(this).find(".right .color9c").text());
        var id=$(this).find(".right span p:eq(0)").attr("id");
        if(id!=null){
            if($("#"+id).text().length==1){
                searchPriceAjax=$.ajax({
                    type: "get",
                    url: "/price/search/search",
                    data:"&id="+id+"&no=" + encodeURIComponent(no)+"&manu="+encodeURIComponent(manu_r)+ "&count="+requestCount+"&date=" + new Date().getTime(),
                    async:true,
                    dataType: "json",
                    success: function(msg) {
                        if(msg!=null){
                            if(msg.v==0){
                                if(requestCount>2){
                                    $("#"+msg.id).parent().parent().parent().find(".tdprice_k_em").text("暂无现货");
                                }
                            }else{
                                var _priceShow="";
                                var _priceAll=msg.v.toFixed(2);
                                var _prices=_priceAll.split(".");
                                if(_prices.length==2){
                                    _priceShow="<span style='font-size:1.3em;color:#fe6c00;'>"+_prices[0]+"</span><span style='font-size:1.3em;color:#fe6c00;'>."+_prices[1]+"</span>";
                                }else{
                                    _priceShow="<span style='font-size:1.1em'></span><span style='font-size:0.8em'>.00</span>";
                                }
                                $("#"+msg.id).html("<span style='font-size:1.3em;color:#fe6c00;'>"+msg.u+"</span>"+_priceShow).css("display","block");
                                $("#"+msg.id).parent().parent().parent().find(".tdprice_k_em").text("现货 "+msg.c+"");
                            }
                        }
                        trid++;
                        setTimeout(function(){getPrice(requestCount,trid);},1);
                    },
                    error:function(XMLHttpRequest,textStatus,errorThrown){
                        trid++;
                        setTimeout(function(){getPrice(requestCount,trid);},1);
                    }
                });
            }else{
                trid++;
                setTimeout(function(){getPrice(requestCount,trid);},1);
            }
        } else{
            trid++;
            setTimeout(function(){getPrice(requestCount,trid);},1);
        }

    });
}

//加载更多
function loadresultAjax(){
    pageNo++;
    var datapara="s="+encodeURIComponent(ToCDB(_searchkeyword))+"&page="+pageNo;
    $.ajax({
        type: "get",
        url: "/m/easydatasheetapi/search/nextpage",
        data:datapara+"&date=" + new Date().getTime(),
        dataType: "json",
        success: function(msg) {
            if(pageNo>=maxpage){
                $(".jiazai").css("display","none");
            }
            if(msg!=null){
                if(searchPriceAjax!=null){
                    searchPriceAjax.abort();
                }
                $("#main:eq(0)").append(loadresultShow(msg));
                imgSize();
                setTimeout(function(){getPrice(1,0);},20);
            }
            loadresultAjaxRun=false;
        },
        error:function(XMLHttpRequest,textStatus,errorThrown){
            //alert(XMLHttpRequest.status);
        }
    });
}
function loadresultShow(models){
    if(models==null){
        return "";
    }
    var str=new StringBuffer();
    for(var g=0;g<models.length;g++){
        for(var m=0;m<models[g].models.length;m++){
            var model= models[g].models[m];
            var _manuShow=model.manu_r.toLocaleUpperCase();
            if(_manuShow.indexOf("unclassified".toLocaleUpperCase())==-1 && _manuShow.indexOf("unclassifed".toLocaleUpperCase())==-1){

            }else{
                _manuShow="其它厂商";
            }
            str.append("<li id='noinfo_noinfo_"+model.id+"'>");
//            str.append("    <div class='left'>");
//            str.append("        <div><img src='"+noPicPath+"/small/"+model.picurls+"'  onerror='javascript:this.src=\""+webpath+"/img_mo/search/mobile_default.png\"' /><p id='price_"+model.id+"' style='display:none;'>&nbsp;</p></div>");
//            str.append("    </div>");
            str.append("    <div class='right'>");
            str.append("        <div class='right-top'>");
            str.append("            <em><a href='javascript:searchopendetail(\""+model.manu_r.toLocaleUpperCase()+"\",\""+model.no+"\");'>"+model.no.toLocaleUpperCase()+"</a></em>");
            if(model.rohs==true){
                str.append("        <img src='"+webpath+"/img_mo/search/rohs_small.png'/>");
            }
            str.append("        </div>");

            str.append("<p class='color9c'>");
            str.append(_manuShow);
            str.append("</p>");


            str.append("<p>");
            if(model.name_cn.length>0){
                str.append(model.name_cn);
            }else{
                str.append(model.name_en);
            }
            str.append("</p>");

            str.append("<span>")
            str.append("<p id='price_"+model.id+"' style='display:none;float: left;'>&nbsp;</p>")
            //现货
            str.append("<em class='tdprice_k_em' style='float: left;'>&nbsp;</em>");
            str.append("</span>")

            if(model.pdfexists==1){
                str.append("<a class='loadpdf' href=\"javascript:void(0);\" onclick=\"javascript:downpdf(this,'/down/"+model.id+".do\',0);\">");
                str.append("<img src='"+webpath+"/img_mo/search/downpdf.png'/>");
                str.append("<span>下载</span></a>");
            }else{
            }


            str.append("    </div>");

            str.append("</li>");
        }
    }

    str.append("<div class='searchpage' style='height:0px;'>");
    str.append("    <span>"+pageNo+"/</span><span>"+maxpage+"</span>");
    str.append("</div>");
    return str.toString();
}
function downpdf(mytag,pdfurl,pdfid){

    //$(mytag).find("span").text("准备");
    $(mytag).attr("href","javascript:void(0);");
    $(mytag).find("img").attr("src",webpath+"/img_mo/search/wait.gif").css("width","100px!important");
    var parent=$(mytag);
    mydownpdf(pdfurl,pdfid,pdfinforesultEvent,parent);

}
function pdfinforesultEvent(msg,pdfid,obj){
    if(msg.type=="error"){
        $(obj).find("span").text("暂无");
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf.png").css("opacity","0.3");
        $(obj).attr("onclick","");
    }else if(msg.type=="nofile"){
        $(obj).find("span").text("暂无");
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf.png").css("opacity","0.3");
        $(obj).attr("onclick","");
    }else{
        $(obj).find("img").attr("src",webpath+"/img_mo/search/downpdf_end.png");
        $(obj).find("span").text("下载");

        $(obj).attr("target","_blank");
        $(obj).attr("href",msg.url);
        $(obj).attr("onclick","");
    }
}
function imgSize(){
    $("#main li .left").each(function(index){
       $(this).find("div:eq(0)").css("height",$(this).find("div:eq(0)").css("width"));
    });
}
